package com.seveneight.baijiesteward.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.BankListBean;
import com.seveneight.baijiesteward.http.HttpConfig;
import com.seveneight.baijiesteward.ui.main.activity.SelectThemeActivity;
import com.seveneight.baijiesteward.views.cover.MyListView;

import java.util.List;

/**
 * 选择标签adapter
 * Created by user1 on 2018/5/31.
 */

public class SelectTableAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<BankListBean> listData;
    Activity activity;
    public SelectTableAdapter(Activity activity, List<BankListBean> listData){
        mInflater = LayoutInflater.from(activity);
        this.activity=activity;
        this.listData=listData;
    }

    @Override
    public int getCount() {
        return null==listData?0:listData.size();
    }
    public void refresh(List<BankListBean> listData){
        this.listData=listData;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return null==listData?0:listData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_select_table_list_layout, null); //加载布局
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
         }
        BankListBean item=listData.get(i);
        holder.item_bank_name.setText(item.getName());
        List<BankListBean> data= JSON.parseArray(item.getTagList(),BankListBean.class);
        if (data.size()>0){
            tableAdapter mAdapter=new tableAdapter(activity,data);
            holder.listView.setAdapter(mAdapter);
            mAdapter.refresh(data);
        }

        return convertView;
    }

    public class ViewHolder{
        MyListView listView;
        TextView item_bank_name;

        ViewHolder(View view){
            listView=view.findViewById(R.id.item_table_list);
            item_bank_name=view.findViewById(R.id.item_bank_name);
        }
    };
    class tableAdapter extends BaseAdapter{
        List<BankListBean> listBeans;
        Activity content;

        public tableAdapter( Activity content,List<BankListBean> listBeans){
            this.content=content;
            this.listBeans=listBeans;
        }

        public void refresh(List<BankListBean> listData){
            this.listBeans=listData;
            this.notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return null==listBeans?0:listBeans.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
            ViewHolder2 holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_select_table_list_tow_layout, null); //加载布局
                holder=new ViewHolder2(convertView);
                convertView.setTag(holder);
            } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
                holder = (ViewHolder2) convertView.getTag();
            }
            final BankListBean bean=listBeans.get(i);
            holder.item_table_name.setText(bean.getName());
            holder.select_icon.setVisibility(bean.getSelect()?View.VISIBLE:View.GONE);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int y=0;y<listBeans.size();y++){
                        if (listBeans.get(y).getSelect()){
                            ((SelectThemeActivity)content).remore(listBeans.get(y));
                        }
                        listBeans.get(y).setSelect(false);
                    }
                    listBeans.get(i).setSelect(true);
                    ((SelectThemeActivity)content).setSeleData(listBeans.get(i));
                    notifyDataSetChanged();

                }
            });
            return convertView;
        }
        public class ViewHolder2{
            TextView item_table_name;
            ImageView select_icon;

            ViewHolder2(View view){
                item_table_name=view.findViewById(R.id.item_table_name);
                select_icon=view.findViewById(R.id.select_icon);
            }
        };
    }
}
